package fragment;

import global.Global;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import pulltorefreshview.RefreshableView;
import pulltorefreshview.RefreshableView.PullToRefreshListener;
import sortlist.SortModel;

import com.example.andoridclient.OrderDetailActivity;
import com.example.andoridclient.R;
import com.example.andoridclient.R.id;



import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class OrderFragment extends Fragment implements OnItemClickListener {
	private static final int LOADING = -1;
	private static final int UNLOADING = -2;
	private static final int FINISHREFRESHING = 1;
	ListView orderlist;
	RefreshableView refreshableView;
	SimpleAdapter listItemAdapter;
	List<HashMap<String, Object>> listItem;
	ProgressDialog progressDialog;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_order, container, false);
		if (view != null) {
			orderlist = (ListView) view.findViewById(R.id.listView1);
			refreshableView = (RefreshableView) view
					.findViewById(R.id.refreshable_view);
			orderlist.setOnItemClickListener(this);
			refreshableView.setOnRefreshListener(new PullToRefreshListener() {
				@Override
				public void onRefresh() {
					listItem = new ArrayList<HashMap<String, Object>>();
					String result = Global.Get("Order/GetOrderList/" + Global.user.ID);
					try {
						JSONArray jsonArray = new JSONArray(result);
						// 解析JSON
						for (int i = 0; i < jsonArray.length(); i++) {
							JSONObject object = (JSONObject) jsonArray.get(i);
							HashMap<String, Object> tmp = new HashMap<String, Object>();
							tmp.put("Header", "订单号：");
							tmp.put("text1", "");
							tmp.put("text2", object.getString("Departure") + "\t送到\t"
									+ object.getString("Destination"));
							tmp.put("text3",
									GetOrderStatus(object.getString("PostmanName"),
											object.getInt("Num")));
							tmp.put("text4", object.getString("SendOutUserName")
									+ "\t送给\t" + object.getString("RecieveUserName"));
							tmp.put("id",object.getString("ID"));
							listItem.add(tmp);
						}
						handler.sendEmptyMessage(FINISHREFRESHING);
					} catch (JSONException e) {
					}
					finally
					{
						refreshableView.finishRefreshing();
					}
				}
			}, 0);
		}
		new Thread(init).start();
		return view;
	}
	
	
	// 线程
	Runnable init = new Runnable() {
		@Override
		public void run() {
			handler.sendEmptyMessage(LOADING);
			listItem = new ArrayList<HashMap<String, Object>>();
			String result = Global.Get("Order/GetOrderList/" + Global.user.ID);
			try {
				JSONArray jsonArray = new JSONArray(result);
				// 解析JSON
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject object = (JSONObject) jsonArray.get(i);
					HashMap<String, Object> tmp = new HashMap<String, Object>();
					tmp.put("Header", "订单号：");
					tmp.put("text1", "");
					tmp.put("text2", object.getString("Departure") + "\t送到\t"
							+ object.getString("Destination"));
					tmp.put("text3",
							GetOrderStatus(object.getString("PostmanName"),
									object.getInt("Num")));
					tmp.put("text4", object.getString("SendOutUserName")
							+ "\t送给\t" + object.getString("RecieveUserName"));
					tmp.put("id",object.getString("ID"));
					listItem.add(tmp);
				}

				 listItemAdapter = new SimpleAdapter(
						getActivity(), listItem,// 数据源
						R.layout.orderitem,// ListItem的XML实现
						// 动态数or与ImageItem对应的子项
						new String[] { "Header", "text1", "text2", "text3",
								"text4","id" },
						// ImageItem的XML文件里面的一个ImageView,两个TextView ID
						new int[] { R.id.textViewid, R.id.hellow,
								R.id.textView2, id.textView03, id.TextView04,id.textViewid});
				orderlist.setAdapter(listItemAdapter);

			} catch (JSONException e) {
				Message message = new Message();
				message.obj = result;
				handler.sendMessage(message);
			} finally {
				handler.sendEmptyMessage(UNLOADING);
			}
		}
	};

	// UI Handler
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case LOADING:
				progressDialog = ProgressDialog.show(getActivity(), "请稍等...",
						"读取中...", true);
				break;
			case UNLOADING:
				progressDialog.dismiss();
				break;
			case FINISHREFRESHING:
				listItemAdapter.notifyDataSetChanged();
				break;
			default:
				break;
			}
		}

	};

	private String GetOrderStatus(String postmanname, int Num) {
		if (TextUtils.isEmpty(postmanname)) {
			return "正在派件中";
		} else if (Num == 0) {
			return "等待快递员接件中";
		} else {
			return "派送中";
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
		Intent intent = new Intent(getActivity(), OrderDetailActivity.class);
		HashMap<String, String> source = (HashMap<String, String>)listItemAdapter.getItem(pos);
		intent.putExtra("orderid", Integer.valueOf(source.get("id")));
		startActivity(intent);
	}
}
